

package com.bei_en.teacher.admin.controller.consultation;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bei_en.teacher.admin.model.consultation.bean.ConsultationPraise;
import com.bei_en.teacher.admin.model.consultation.condition.ConsultationPraiseCondition;
import com.bei_en.teacher.admin.model.consultation.condition.NEWConsultationPraiseCondition;
import com.bei_en.teacher.admin.model.consultation.vo.ConsultationCollectionVo;
import com.bei_en.teacher.admin.model.consultation.vo.ConsultationPraiseVo;
import com.bei_en.teacher.admin.service.consultation.ConsultationPraiseService;
import com.bei_en.teacher.admin.controller.BaseController;
import com.bei_en.common.util.interceptor.OAuthRequired;
import com.bei_en.common.web.JsonFailResult;
import com.bei_en.common.web.JsonResult;
import com.bei_en.common.web.JsonSuccessResult;
import com.mysql.fabric.xmlrpc.base.Array;
/**
 * 
 * @author Guocg
 * @date 2016-8-3 23:46:19
 */
@Controller
@RequestMapping("/api/Consultation")
public class ConsultationPraiseAPPController extends BaseController {

    @Autowired
    ConsultationPraiseService consultationPraiseService;


    @RequestMapping("/consultationPraise")
	@ResponseBody
	@OAuthRequired
    public JsonResult consultationPraise(Model model,ConsultationPraiseCondition vo, BindingResult br, HttpSession session ) {

		List<ConsultationPraiseVo> list =new ArrayList<ConsultationPraiseVo>();
		   list = consultationPraiseService.findConsultationPraisePage(vo);

	
		return new JsonSuccessResult(session.getId(), list);
    }

    @RequestMapping("/consultationPraiseAdd")
    @ResponseBody
    @OAuthRequired
    public JsonResult consultationPraiseAdd(Model model,@Valid NEWConsultationPraiseCondition vo, BindingResult br, HttpSession session) {
      
 	if (br!=null&&br.hasErrors()){
   		
   		return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
				.getDefaultMessage());
   	}
	try {
		ConsultationPraiseVo consultationPraiseVo=new ConsultationPraiseVo();
		consultationPraiseVo.setPraisePeople(vo.getPraisePeople());
		consultationPraiseVo.setConsultatinId(vo.getConsultatinId());
		ConsultationPraiseCondition condition=new ConsultationPraiseCondition();
		condition.setPraisePeople(vo.getPraisePeople());
		condition.setConsultatinId(vo.getConsultatinId());
		 List<ConsultationPraiseVo> consultationPraiseVos=	consultationPraiseService.findConsultationPraisePage(condition);
			if(!consultationPraiseVos.isEmpty()){
				ConsultationPraiseVo collectionVo=null;
				for (int i = 0; i < consultationPraiseVos.size(); i++) {
					collectionVo=consultationPraiseVos.get(i);
					consultationPraiseService.delete(collectionVo.getId());
				}
				return new JsonSuccessResult(session.getId(), "取消点赞成功");
			}
		
   	consultationPraiseVo.setUserId(vo.getUserId());
   	consultationPraiseVo.setCreateTime(new Date());
   	consultationPraiseService.insert(consultationPraiseVo);
   	return new JsonSuccessResult(session.getId(), "成功");
	} catch (Exception e) {
		e.printStackTrace();
		return new JsonFailResult(session.getId(), 1, "后台报错",
				e.getMessage());
	}
    }

    @RequestMapping("/consultationPraiseEdit")
    public JsonResult consultationPraiseEdit(Model model,ConsultationPraiseCondition vo, BindingResult br, HttpSession session) {
	
    	ConsultationPraiseVo consultationPraise=new ConsultationPraiseVo();
	
		consultationPraiseService.updateSelective(consultationPraise);
	
		return new JsonSuccessResult(session.getId(), 	new Array());
    }

  
    
}
